package com.talent.controller;


import com.talent.interceptor.FormToken;
import com.talent.interceptor.Token;
import com.talent.model.SysUser;
import com.talent.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/sysUserController")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    @RequestMapping("/showUserToJspById/{userId}")
    public String showUser(Model model, @PathVariable("userId") Long userId) {
        SysUser user = this.sysUserService.getById(userId);
        model.addAttribute("user", user);
        return "showUser";
    }

    @RequestMapping("/showUserToJSONById/{userId}")
    @ResponseBody
    public SysUser showUser(@PathVariable("userId") Long userId) {
        SysUser user = sysUserService.getById(userId);
        return user;
    }

    @RequestMapping("/toAddSysUserPage")
    @Token(save = true)
    public String toAddSysUserPage() {
        return "addUser";
    }

    @RequestMapping("/addSysUser")
    @ResponseBody
    @Token(remove = true)
    public SysUser addSysUser(SysUser sysUser) {
        return sysUserService.add(sysUser);
    }


    /**
     * 作为参数传递的token格式为：addSysUserAOP_UUID,在FormTokenAspect中首先判断token的前一部分跟FormToken注解上的method的值是否相同，如果相同再去判UUID的值是否和redis中的相同。
     *
     * @param token
     * @param sysUser
     * @return
     */
    @RequestMapping("/addSysUserAOP")
    @ResponseBody
    @FormToken(method = "addSysUserAOP", param = "#token")
    public SysUser addSysUserAOP(String token, SysUser sysUser) {
        System.out.println(token);
        return sysUserService.add(sysUser);
    }

}
